package com.aiti.lulian.service.impl;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.page.PageInfo;
import com.aiti.base.core.page.PageResult;
import com.aiti.base.core.utils.message.Message;

import com.aiti.lulian.dto.CardTemplateBackgroundDto;
import com.aiti.lulian.entity.CardTemplateBackground;

import com.aiti.lulian.entity.newCard.CardBaseInfo;
import com.aiti.lulian.gxmh.vo.GxmhArticleVo;
import com.aiti.lulian.mapper.CardTemplateBackgroundMapper;
import com.aiti.lulian.mapper.newCard.CardPersonalBaseInfoMapper;
import com.aiti.lulian.service.CardTemplateBackgroundService;
import com.aiti.lulian.vo.newCard.UserCardRoleVo;
import com.aiti.lulian.vo.newCard.UserCurrentUseCardInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CardTemplateBackgroundServiceImpl
        extends ServiceImpl<CardTemplateBackgroundMapper, CardTemplateBackground>
        implements CardTemplateBackgroundService {

    @Autowired
    private CardPersonalBaseInfoMapper cardPersonalBaseInfoMapper;

    @Override
    public Message saveBackground(CardTemplateBackground entity) {
        entity.setCustomFlag(true);
        this.save(entity);
        return Message.success(entity.getBaseId());
    }

    @Override
    public Message updateBackground(CardTemplateBackground entity) {
        this.updateById(entity);
        return Message.success();
    }

    @Override
    public Message deleteBackground(String id) {
        this.removeById(id);
        return Message.success();
    }

    @Override
    public Message detail(String id) {
        CardTemplateBackground entity = this.getById(id);
        return Message.success(entity);
    }

    @Override
    public Message saveBackgroundBatch(List<CardTemplateBackground> list) {
        this.saveBatch(list);
        return Message.success();
    }


    @Override
    public Message backgroundList(Page page, CardTemplateBackgroundDto dto) {
        String createCardId = dto.getCreateCardId();
        if(StringUtils.isEmpty(createCardId)){
            return Message.fail("传参错误，缺少名片信息");
        }
        CardBaseInfo cardBaseInfo = cardPersonalBaseInfoMapper.selectById(createCardId);

        if(cardBaseInfo == null){
            return Message.fail("名片不存在");
        }


        LambdaQueryWrapper<CardTemplateBackground> wrapper = new LambdaQueryWrapper<>();

        String styleId = dto.getStyleId();
        if(StringUtils.isEmpty(styleId)){
            return Message.fail("请选择样式");
        }
//        if(StringUtils.isEmpty(dto.getTemplateId())){
//            wrapper.isNull(CardTemplateBackground::getTemplateId);
//        }else{
//            wrapper.and(w -> w.isNull(CardTemplateBackground::getTemplateId).or()
//                    .eq(CardTemplateBackground::getTemplateId, dto.getTemplateId()));
//        }
//        wrapper.and(w -> w.isNull(CardTemplateBackground::getTemplateId).or()
//                    .eq(CardTemplateBackground::getEnterpriseId, dto.getEnterpriseId()));

        wrapper.and(w -> w.isNull(CardTemplateBackground::getTemplateId)
                .or(andWrapper -> andWrapper
                        .eq(CardTemplateBackground::getEnterpriseId, cardBaseInfo.getEnterpriseId())

                )
        );
        wrapper.eq(dto.getCustomFlag() != null, CardTemplateBackground::getCustomFlag, dto.getCustomFlag()).eq(CardTemplateBackground::getStyleId, dto.getStyleId()).eq(CardTemplateBackground::getTemplateType, dto.getTemplateType())
                .orderByAsc(CardTemplateBackground::getBaseCreateTime);
        Page page1 = this.page(page, wrapper);
        PageResult pageResult = new PageResult<>(page1.getRecords(), new PageInfo(dto.getPageNum(), dto.getPageSize()), page1.getTotal());

        return Message.success(pageResult);
    }

    @Override
    public Message savePreBackground(CardTemplateBackground cardTemplateBackground) {

        cardTemplateBackground.setCustomFlag(false);
        this.save(cardTemplateBackground);
        return Message.success(cardTemplateBackground.getBaseId());
    }

    @Override
    public Message preBackgroundList(Page page, CardTemplateBackgroundDto dto) {
        LambdaQueryWrapper<CardTemplateBackground> wrapper = new LambdaQueryWrapper<>();

        String styleId = dto.getStyleId();
        Page page1 = new Page();
        if(!StringUtils.isEmpty(styleId)){
            wrapper.and(w -> w.isNull(CardTemplateBackground::getTemplateId)
                            .or(andWrapper -> andWrapper
                                    .eq(CardTemplateBackground::getTemplateId, dto.getTemplateId())

                            )
                    )
                    .eq(CardTemplateBackground::getStyleId, dto.getStyleId())
                    .eq(!StringUtils.isEmpty(dto.getVersion()),CardTemplateBackground::getVersion, dto.getVersion())
                    .eq(CardTemplateBackground::getCustomFlag, false)
                    .orderByAsc(CardTemplateBackground::getBaseCreateTime);
            page1 = this.page(page, wrapper);
        }
        PageResult pageResult = new PageResult<>(page1.getRecords(), new PageInfo(dto.getPageNum(), dto.getPageSize()), page1.getTotal());
        return Message.success(pageResult);
    }
}